package by.wind.drebedengi.datalayer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map.Entry;
import java.util.Vector;

import java.util.Iterator;
import by.wind.drebedengi.datalayer.bean.Currency;

public class GetCurrencyList extends AbstractAction{
	
	public static final String SOAP_ACTION = "getCurrencyList";
	public static final String METHOD_NAME = "getCurrencyList";
	
	
	
	private ArrayList<Currency> itsCurrencyList = new ArrayList<Currency>();
	
	
	@Override
	public void loadData() throws Exception{
		HashMap<String, Object> propertyList = new HashMap<String, Object>();
		propertyList.put(Constants.API_ID_KEY, Constants.API_ID_VALUE);
		propertyList.put(Constants.LOGIN_KEY, Constants.LOGIN_VALUE);
		propertyList.put(Constants.PASS_KEY, Constants.PASS_VALUE);
		
		Vector<Hashtable<String,String>> itsData = (Vector<Hashtable<String,String>>) loadData(METHOD_NAME, SOAP_ACTION, propertyList);
		for(int i = 0 ; i< itsData.size() ; i ++) {
			itsCurrencyList.add(convertHashtableToCurrency(itsData.get(i)));
		}
	}
	
	
	
	
	public Currency convertHashtableToCurrency(Hashtable<String,String> element) {
		Currency currencyElement = new Currency();
		
		
		Iterator<Entry<String, String>> iterator = element.entrySet().iterator();
		while(iterator.hasNext()) {
			
			String key = iterator.next().getKey();
			String value = element.get(key);
			if (key.equals(Currency.ID)) {
				currencyElement.setServer_id(Integer.parseInt(value));
			} else if (key.equals(Currency.NAME)) {
				currencyElement.setName(value);
			} else if (key.equals(Currency.COURSE)) {
				currencyElement.setCource(Float.parseFloat(value));
			} else if (key.equals(Currency.CODE)) {
				currencyElement.setCode(value);
			} else if (key.equals(Currency.FAMILY_ID)) {
				currencyElement.setFamilyId(Integer.valueOf(value));
			} else if (key.equals(Currency.DEFAULT)) {
				if (value.equals("t"))
					currencyElement.setDefault(true);
			} else if (key.equals(Currency.AUTOUPDATE)) {
				if (value.equals("t"))
					currencyElement.setAutoupdate(true);
			} else if (key.equals(Currency.HIDDEN)) {
				if (value.equals("t")) {
					currencyElement.setHidden(true);
				}
			}
			
		}
		
		return currencyElement;
	}
	
	public ArrayList<Currency> getCurrencyList() {
		return itsCurrencyList;
	}
}
